package com.doraemon.controller.user;

import com.doraemon.dto.OrdersPageQueryDTO;
import com.doraemon.dto.OrdersPaymentDTO;
import com.doraemon.dto.OrdersSubmitDTO;
import com.doraemon.result.PageResult;
import com.doraemon.result.Result;
import com.doraemon.service.OrderService;
import com.doraemon.vo.OrderPaymentVO;
import com.doraemon.vo.OrderSubmitVO;
import com.doraemon.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * c端订单管理Controller
 **/
@RestController("userOrderController") // 设置自定义 Bean 名称，防止同名冲突)
@RequestMapping("/user/order")
@Slf4j
@Api(tags = "订单相关接口")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /***
     * 用户下单
     * ---
     * @param ordersSubmitDTO 订单提交DTO类（前端参数）
     * @return: com.doraemon.result.Result<com.doraemon.vo.OrderSubmitVO> 订单提交VO类
     */
    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public Result<OrderSubmitVO> submitOrder(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        log.info("c端-用户下单：{}", ordersSubmitDTO);

        // 调用Service层 submitOrder 方法，获取订单提交VO类
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);

        return Result.success(orderSubmitVO);
    }

    /***
     * 订单支付
     * ---
     * @param ordersPaymentDTO 订单支付DTO类（前端参数）
     * @return: com.doraemon.result.Result<com.doraemon.vo.OrderPaymentVO> 订单支付VO类
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("c端-订单支付：{}", ordersPaymentDTO);

        // 调用Service层 payment 方法，获取订单支付VO类
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);

        return Result.success(orderPaymentVO);
    }

    /***
     * 历史订单分页查询
     * ---
     * @param ordersPageQueryDTO 订单分页DTO类
     * @return: com.doraemon.result.Result<com.doraemon.result.PageResult>
     */
    @GetMapping("/historyOrders")
    @ApiOperation("历史订单查询")
    public Result<PageResult> historyOrders(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("c端-历史订单查询");

        // 调用Service层 pageQuery 方法，获取查询到的分页结果集对象pageResult
        PageResult pageResult = orderService.pageQuery(ordersPageQueryDTO);

        return Result.success(pageResult);
    }

    /*    @GetMapping("/historyOrders")
    @ApiOperation("历史订单查询")
    public Result<PageResult> historyOrders(int page, int pageSize, Integer status) {
        log.info("c端-历史订单查询");

        // 调用Service层 pageQuery 方法，获取查询到的分页结果集对象pageResult
        PageResult pageResult = orderService.pageQuery(page, pageSize, status);

        return Result.success(pageResult);
    }*/

    /***
     * 查询订单详情
     * ---
     * @param orderId 订单id
     * @return: com.doraemon.result.Result<com.doraemon.vo.OrderVO>
     */
    @GetMapping("/orderDetail/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> details(@PathVariable("id") Long orderId) {
        log.info("c端-查询订单详情：{}", orderId);

        // 调用Service层 details 方法，获取查询到的OrderVO类
        OrderVO orderVO = orderService.details(orderId);

        return Result.success(orderVO);
    }

    /***
     * 取消订单
     * ---
     * @param orderId 订单id
     * @return: com.doraemon.result.Result
     */
    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public Result cancelOrder(@PathVariable("id") Long orderId) {
        log.info("c端-取消订单：{}", orderId);

        // 调用Service层 CancelOrder 方法
        orderService.cancelOrder(orderId);

        return Result.success();
    }

    /***
     * 再来一单
     * ---
     * @param orderId 订单id
     * @return: com.doraemon.result.Result
     */
    @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单")
    public Result reorder(@PathVariable("id") Long orderId) {
        log.info("c端-再来一单：{}", orderId);

        // 调用Service层 reorder 方法
        orderService.reorder(orderId);

        return Result.success();
    }

    /***
     * 用户催单
     * ---
     * @param orderId 订单id
     * @return: com.doraemon.result.Result
     */
    @GetMapping("/reminder/{id}")
    @ApiOperation("用户催单")
    public Result reminder(@PathVariable("id") Long orderId) {
        log.info("c端-用户催单：{}", orderId);

        // 调用Service层 reminder 方法
        orderService.reminder(orderId);

        return Result.success();
    }


}
